//
//  LBMediaPresentViewController.m
//  LangBa
//
//  Created by Mac on 2017/12/21.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "LBPassWordPresentViewController.h"
#import "STPasswordTextField.h"
@interface LBPassWordPresentViewController ()
@property(nonatomic, strong) STButton                     *clearButton;
@property(nonatomic, strong) UIView                     *customView;
@property(nonatomic, strong) STPasswordTextField                     *textFiled;

@end

@implementation LBPassWordPresentViewController

- (instancetype)init
{
    if (self == [super init]) {
        self.modalPresentationStyle = UIModalPresentationOverFullScreen;
    }
    return self;
}
#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    [self configSubView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.textFiled becomeFirstResponder];
    [self show];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.clearButton = [[STButton alloc] initWithFrame:UIScreenFrame
                                                 title:nil
                                            titleColor:nil
                                             titleFont:0
                                          cornerRadius:0
                                       backgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.4]
                                       backgroundImage:nil
                                                 image:nil];
    [weakSelf.clearButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedWhiteView];
    }];
    [self.view addSubview:_clearButton];
    
    self.customView = [[UIView alloc] initWithFrame:CGRectMake(0, UIScreenHeight, UIScreenWidth - 60, 342)];
    self.customView.centerX = UIScreenWidth / 2;
    self.customView.top = UIScreenHeight;
    self.customView.backgroundColor = [UIColor whiteColor];
    self.customView.layer.cornerRadius = 10;
    self.customView.clipsToBounds = YES;
    [self.clearButton addSubview:self.customView];
    UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onSelectedWhiteView)];
    self.customView.userInteractionEnabled = YES;
    [self.customView addGestureRecognizer:ges];
    
    //sub
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 20, self.customView.width, 30)
                                                     text:@"请输入6位数字密码"
                                                textColor:FlatBlack
                                                     font:18
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    [self.customView addSubview:titleLable];
    
    self.textFiled = [[STPasswordTextField alloc] initWithFrame:CGRectMake(0, titleLable.bottom  +10, self.customView.width - 42 , 44)
                                                  passwordLenth:6];
    self.textFiled.centerX = self.customView.width / 2;
    self.textFiled.secureTextEntry = YES;
    [self.textFiled setTextFieldDidInputMaxLenth:^(NSString *password) {
        NSLog(@"%@",password);
       // [weakSelf textFieldDidInput:password];
    }];
    [self.customView addSubview:self.textFiled];
    
   self.alertLable = [[STLabel alloc] initWithFrame:CGRectMake(self.textFiled.left, self.textFiled.bottom  +10, 200, 20)
                                                     text:@"密码错误"
                                                textColor:TM_redColor
                                                     font:13
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    self.alertLable.hidden = YES;
    [self.customView addSubview:self.alertLable];
    CGFloat bootom = self.textFiled.bottom + 30;
    
    STButton * cancleButton = [[STButton alloc] initWithFrame:CGRectMake(0, bootom + 30, self.customView.width / 2, 44)
                                                        title:@"取消"
                                                   titleColor:TM_orangeColor
                                                    titleFont:15
                                                 cornerRadius:10
                                              backgroundColor:nil
                                              backgroundImage:nil
                                                        image:nil];
    [cancleButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedCancleButton];
    }];
    [cancleButton st_showRightLine:cancleButton.height];
    cancleButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [cancleButton st_showTopLine];
    [self.customView addSubview:cancleButton];
    
    
    STButton * finshButton = [[STButton alloc] initWithFrame:CGRectMake(self.customView.width / 2, bootom + 30, self.customView.width / 2, 44)
                                                       title:@"确定"
                                                  titleColor:TM_orangeColor
                                                   titleFont:15
                                                cornerRadius:10
                                             backgroundColor:nil
                                             backgroundImage:nil
                                                       image:nil];
    [finshButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedCommitButton];
    }];
    finshButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [finshButton st_showTopLine];
    [self.customView addSubview:finshButton];
    
    self.customView.height = finshButton.bottom;
    
}
#pragma mark --Private Method
- (void)show{
    [UIView animateWithDuration:0.6
                          delay:0
         usingSpringWithDamping:0.6
          initialSpringVelocity:0.7
                        options:UIViewAnimationOptionLayoutSubviews
                     animations:^{
                         self.customView.centerY = UIScreenHeight / 2;
                     } completion:^(BOOL finished) {
                        
                     }];
}
- (void)dismiss{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.customView.top = UIScreenHeight;
        self.clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
}
- (void)dismissWithHandle:(void(^)())handle{
    [self.view endEditing:YES];
    [UIView animateWithDuration:0.25 animations:^{
        self.customView.top = UIScreenHeight;
        self.clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (handle) {
                handle();
            }
        }];
    }];
}
#pragma mark --Action Method
- (void)onSelectedCommitButton{
    
    if (self.textFiled.text.length != self.textFiled.passwordLenth) {
        [SVProgressHUD showErrorWithStatus:@"请输入六位密码"];
        return;
    }
    [self textFieldDidInput:self.textFiled.text];
    
}
- (void)onSelectedCancleButton{
    
    [self dismiss];
    
}
- (void)onSelectedWhiteView{
    [self.view endEditing:YES];
}
- (void)textFieldDidInput:(NSString*)pwd{
    //判断密码不正确，显示动画
    __weak typeof(self) weakSelf =  self;
    if (!self.pwd) {
        //如果没传入密码，则为设置密码
        if (self.pwdDidEdited) {
             self.pwdDidEdited(weakSelf,self.textFiled.text);
           
        }
    }else{
        if ([self.pwd isEqualToString:self.textFiled.text]) {
            //密码正确
            self.alertLable.hidden = YES;
            self.pwdDidEdited(weakSelf,self.textFiled.text);
        }else{
            self.alertLable.hidden = NO;
            self.pwdDidEdited(weakSelf,self.textFiled.text);
            //[self rightLablebackAnimation];
        }

    }

}
- (void)rightLablebackAnimation{

    [UIView animateWithDuration:0.15 animations:^{
        self.alertLable.left = self.textFiled.left  + 30;
    } completion:^(BOOL finished) {
        [self alertLablebackAnimation];
    }];
}
- (void)alertLablebackAnimation{
    [UIView animateWithDuration:0.16 delay:0
         usingSpringWithDamping:0.05 initialSpringVelocity:8 options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         self.alertLable.left = self.textFiled.left ;
                     } completion:^(BOOL finished) {
                         
                     }];
}

@end


